// 设置本地存储
export const setLocalStorage = (key, vaule) => {
  return localStorage.setItem(key, JSON.stringify(vaule));
};
// 获取本地存储
export const getLocalStorage = (key) => {
  return JSON.parse(localStorage.getItem(key));
};
// 清除本地存储
export const removeLocalStorage = (key) => {
  localStorage.removeItem(key);
};
// 判断视频是否在浏览器支持播放
export const canPlayVideo = (url) => {
  return new Promise((resolve, reject) => {
    const video = document.createElement("video");
    video.src = url;
    video.addEventListener("canplaythrough", () => {
      resolve(true);
      video.remove();
    });
    video.addEventListener("error", () => {
      resolve(false);
      video.remove();
    });
    document.body.appendChild(video);
  });
};
// 视频 或 图片 在浏览器播放 / 下载
export const viewVideoOrImg = (src) => {
  // 创建一个下载链接
  const downloadLink = document.createElement("a");
  downloadLink.href = src;
  downloadLink.target = "_blank";
  // 设置下载链接的显示名称
  // downloadLink.download = 'downloaded-image.png';

  // 模拟用户点击下载链接
  downloadLink.click();
};
/**
 * 将oss 链接形式的视频 下载到本地
 * @param {*} url 链接地址
 * @param {*} name 文件名称
 */
export const downVideo = (url, name) => {
  var xhr = new XMLHttpRequest();
  xhr.open("GET", url, true);
  xhr.responseType = "arraybuffer"; // 返回类型blob
  xhr.onload = function () {
    if (xhr.readyState === 4 && xhr.status === 200) {
      let blob = this.response;
      // 转换一个blob链接
      let u = window.URL.createObjectURL(
        new Blob([blob], { type: "video/mp4" })
      );
      let a = document.createElement("a");
      a.download = name;
      a.href = u;
      a.style.display = "none";
      document.body.appendChild(a);
      a.click();
      a.remove();
    }
  };
  xhr.send();
};
/**
 * 点击复制
 * @param {复制的内容} text 
 * @param {this} that 
 */
export const clickCopy = (text, that) => {
  const save = function(evn) {
    evn.clipboardData.setData('text/plain', text)
    evn.preventDefault() // 阻止默认行为
  }
  document.addEventListener('copy', save) // 添加一个copy事件
  document.execCommand('copy') // 执行copy方法
  that.$message({ message: '复制成功', type: 'success' })
}
/**
 * 两个对象数组去重（需要存在id）
 * @param {数组1} arr1 
 * @param {数组2} arr2 
 * @returns 
 */
export const removeRepeat = (arr1 = [], arr2 = []) => {
  return [...new Map([...arr1, ...arr2].map(item => [item.id, item])).values()]
}
// 深拷贝
export const  deepClone = (obj) => {
  let newObj;
  // 先判断数据类型
  if (obj.constructor === Array) {
    // 数组
    newObj = [];
    for (const item of obj) {
      newObj.push(item);
    }
  } else if (obj.constructor === Object) {
    // 对象
    newObj = {};
    for (let key in obj) {
      newObj[key] = deepClone(obj[key]);
    }
  } else {
    return obj;
  }
  return newObj;
}
/**
 * 生成随机色值
 */
const randomRGB = () => {
  const r = Math.floor(Math.random() * 255);
  const g = Math.floor(Math.random() * 255);
  const b = Math.floor(Math.random() * 255);
  return `rgb(${r}, ${g}, ${b})`;
};
/**
 * 判断当前是否为移动设备
 */
export const isMobileTerminal = computed(() => {
  return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
     navigator.userAgent
  )
})
